package com.dotflat.xj.controller;

import com.dotflat.rest.controller.RestfulBaseController;
import com.dotflat.xj.VO.DataVO;
import com.dotflat.xj.domain.Data;
import com.dotflat.xj.po.DataPO;
import com.dotflat.xj.service.DataService;
import com.dotflat.xj.service.TargetService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * 巡检字典Controller
 * 
 * @author #
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/api/xj_data")
@Slf4j
@Api(tags = "巡检 - 数据")
public class XJDataController extends RestfulBaseController<Data, DataService, DataPO>
{

    @Autowired
    private  DataService dataService;

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    public void setBaseService(DataService service) {
        super.setBaseService(service);
    }

    //检查对象类别
    private static  final String  XJ_TARGET_TYPE = "xj_target_type";
    //检查分类
    private static  final String  XJ_SORT = "xj_sort";
    //检查结论类别
    private static  final String  XJ_CONCLUSION_TYPE = "xj_conclusion_type";
    //检查周期
    private static  final String  XJ_CYCLE = "xj_cycle";
    //记录类型
    private static  final String  XJ_RECORD_TYPE= "xj_record_type";


    @ApiOperation(value = "添加字典数据")
    @PostMapping("/add_data")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "label", value = "标签", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "val", value = "值", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型", required = true, dataType = "string")

    })
    public StatusResponse addData(@RequestHeader(TICKET) String ticket, String label, String val, String type){
        Data data = new Data();
        StaffDTO staff = cache.getStaff(ticket);
        data.setAreaId(staff.getAreaId());
        data.setCreatedBy(staff.getId());
        data.setLabel(label);
        data.setVal(val);
        data.setVisible(String.valueOf(0));
        data.setDeleted(false);
        data.setType(type);
        return StatusResponse.success(dataService.addData(data));
    }



    @ApiOperation(value = "菜单集合")
    @PostMapping("/all_list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
    })
    public StatusResponse getTargetData(@RequestHeader(TICKET) String ticket){
        StaffDTO staff = cache.getStaff(ticket);
        String areaId = staff.getAreaId();

        Map map = new HashMap();

        //检查对象类别字典
        List<Data>  list2 = dataService.findAllByType(XJ_TARGET_TYPE);
        //获取检查分类字典
        List<Data>  list3 = dataService.findAllByType(XJ_SORT);
        //获取检查结论类别字典
        List<Data>  list4 = dataService.findAllByType(XJ_CONCLUSION_TYPE);
        List<DataVO> list8 = new ArrayList<>();
        for (int i = 0; i <list4.size() ; i++) {
            DataVO dataVO = new DataVO();
            CopyUtils.copyProperties(list4.get(i), dataVO);
            List<Data>  list7 = dataService.typeByData( list4.get(i).getLabel(),areaId);
            dataVO.setDataList(list7);
            list8.add(dataVO);
        }
        //获取检查周期字典
        List<Data>  list5 = dataService.findAllByType(XJ_CYCLE);
        //获取检查记录类型字典
        List<Data>  list6 = dataService.findAllByType(XJ_RECORD_TYPE);

        map.put("xj_target_type",list2);
        map.put("xj_sort",list3);
        map.put("xj_conclusion_type",list8);
        map.put("xj_cycle",list5);
        map.put("xj_record_type",list6);

        return StatusResponse.success(map);
    }


    @ApiOperation(value = "获取单条")
    @PostMapping("/get_data")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = true, dataType = "string")
    })
    public StatusResponse getData(String id){
        return StatusResponse.success(dataService.getData(id));
    }



    @ApiOperation(value = "类型 - 列表")
    @PostMapping("/get_type")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型", required = true, dataType = "string")
    })
    public StatusResponse findAllByType(String type){
        List<Data>  list = dataService.findAllByType(type);
        return StatusResponse.success(list);
    }



}
